﻿using System;
using System.Collections.Generic;

namespace BizElements.Generator
{
    public interface IVariableValueBus
    {
        string PullVariableValue(string variableName);
        event EventHandler<EventArgs<Pair<string, string>>> VariableValuePushed;
        void PushVariableValue(string name, string value);
    }

    sealed class VariableValueBus : IVariableValueBus
    {
        Delegates.Function<string, string> variableValueGetter;
        SortedList<string, string> suppresedVariables = new SortedList<string, string>();

        public VariableValueBus(Delegates.Function<string, string> variableValueGetter, string[] suppresedVariables)
        {
            if (variableValueGetter == null)
                throw new ArgumentNullException("Variable value getter method is required.");

            this.variableValueGetter = variableValueGetter;
            foreach (string varName in suppresedVariables)
                this.suppresedVariables.Add(varName, varName);
        }

        public string PullVariableValue(string variableName)
        {
            return this.variableValueGetter(variableName);
        }

        public event EventHandler<EventArgs<Pair<string, string>>> VariableValuePushed;

        public void PushVariableValue(string name, string value)
        {
            if (this.suppresedVariables.ContainsKey(name))
                return;

            if (this.VariableValuePushed != null)
            {
                Pair<string, string> nameValuePair = new Pair<string, string>(name, value);
                EventArgs<Pair<string, string>> evtItem = new EventArgs<Pair<string, string>>(nameValuePair);
                VariableValuePushed(this, evtItem);
            }
        }
    }
}
